package cn.iocoder.yudao.module.crm.controller.admin.followup;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowStageConfigRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowStageConfigSaveReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowTypeConfigRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowTypeConfigSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.followup.CrmFollowStageConfigDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.followup.CrmFollowTypeConfigDO;
import cn.iocoder.yudao.module.crm.service.followup.CrmFollowStageConfigService;
import cn.iocoder.yudao.module.crm.service.followup.CrmFollowTypeConfigService;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 跟进阶段配置")
@RestController
@RequestMapping("/crm/follow-stage-config")
@Validated
public class CrmFollowStageConfigController {

    @Resource
    private CrmFollowStageConfigService followStageConfigService;


    @PostMapping("/create")
    @Operation(summary = "创建跟进类型")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Long> createConfig(@Valid @RequestBody CrmFollowStageConfigSaveReqVO createReqVO) {
        return success(followStageConfigService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跟进类型")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Long> updateConfig(@Valid @RequestBody CrmFollowStageConfigSaveReqVO createReqVO) {
        return success(followStageConfigService.updateConfig(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        followStageConfigService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "查询配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CrmFollowStageConfigRespVO> getConfig(@RequestParam("id") Long id) {
        CrmFollowStageConfigDO followUpRecord = followStageConfigService.getConfig(id);

        return success(BeanUtils.toBean(followUpRecord, CrmFollowStageConfigRespVO.class));
    }


    @GetMapping("/simple-list")
    @Operation(summary = "查询列表")
    public CommonResult<List<CrmFollowStageConfigRespVO>> getConfigList(@RequestParam(name = "moduleId", required = false) Long moduleId) {
        List<CrmFollowStageConfigDO> followUpRecord = followStageConfigService.getListByModuleId(moduleId);
        return success(BeanUtils.toBean(followUpRecord, CrmFollowStageConfigRespVO.class, vo -> {
            vo.isCommonSetter();
        }));
    }


    @PostMapping("/create-default")
    @Operation(summary = "创建跟进类型-系统配置")
    @PreAuthorize("@ss.hasPermission('crm:module:update-default')")
    public CommonResult<Long> createConfigDefault(@Valid @RequestBody CrmFollowStageConfigSaveReqVO createReqVO) {
        return success(TenantUtils.execute(0L, () -> followStageConfigService.createConfig(createReqVO)));
    }

    @PutMapping("/update-default")
    @Operation(summary = "更新跟进类型-系统配置")
    @PreAuthorize("@ss.hasPermission('crm:module:update-default')")
    public CommonResult<Long> updateConfigDefault(@Valid @RequestBody CrmFollowStageConfigSaveReqVO createReqVO) {
        return success(TenantUtils.execute(0L, () -> followStageConfigService.updateConfig(createReqVO)));
    }

    @DeleteMapping("/delete-default")
    @Operation(summary = "删除配置-系统配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:module:update-default')")
    public CommonResult<Boolean> deleteConfigDefault(@RequestParam("id") Long id) {
        TenantUtils.execute(0L, () -> followStageConfigService.deleteConfig(id));
        return success(true);
    }

    @GetMapping("/get-default")
    @Operation(summary = "查询配置-系统配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CrmFollowStageConfigRespVO> getConfigDefault(@RequestParam("id") Long id) {
        CrmFollowStageConfigDO followUpRecord = TenantUtils.execute(0L, () -> followStageConfigService.getConfig(id));
        return success(BeanUtils.toBean(followUpRecord, CrmFollowStageConfigRespVO.class));
    }


    @GetMapping("/simple-list-default")
    @Operation(summary = "查询列表")
    @PreAuthorize("@ss.hasPermission('crm:module:query-default')")
    public CommonResult<List<CrmFollowStageConfigRespVO>> getConfigList() {
        List<CrmFollowStageConfigDO> followUpRecord = TenantUtils.execute(0L, () -> followStageConfigService.getListByModuleId(0L));
        return success(BeanUtils.toBean(followUpRecord, CrmFollowStageConfigRespVO.class, vo -> {
            vo.isCommonSetter();
        }));
    }

}
